﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using ZuesControls.Controls;
using ZuesControls.Event.DragDrop;

namespace ZuesControls.Event
{
    internal class InputSubscriberBase
    {
        protected internal bool IsLeftButtonDown { get; set; }
        internal protected ZSTreeView TreeView { get; internal set; }

        internal virtual void Initialized()
        {
        }

        internal virtual void OnScrollChanged(ScrollChangedEventArgs e)
        {
        }

        internal virtual void OnMouseDown(MouseButtonEventArgs e)
        {
        }

        internal virtual void OnMouseUp(MouseButtonEventArgs e)
        {
        }

        internal virtual void OnMouseMove(MouseEventArgs e)
        {
        }

        internal Point GetMousePosition()
        {
            Point currentPoint = Mouse.GetPosition(TreeView);
            return currentPoint;
        }
        internal Point GetMousePositionRelativeToContent()
        {
            Point currentPoint = Mouse.GetPosition(TreeView);

            currentPoint = new Point(currentPoint.X + TreeView.ScrollViewer.ContentHorizontalOffset, currentPoint.Y + TreeView.ScrollViewer.ContentVerticalOffset);
            return currentPoint;
        }
        /// <summary>
        /// 这是鼠标在悬浮时，尝试寻找元素并且把他转化为装饰器
        /// </summary>
        /// <param name="positionRelativeToTree"></param>
        /// <returns></returns>
        protected bool IsMouseOverAdorner(Point positionRelativeToTree)
        {
            HitTestResult hitTestResult = VisualTreeHelper.HitTest(TreeView, positionRelativeToTree);
            if (hitTestResult == null || hitTestResult.VisualHit == null) return false;

            Adorner item = null;
            DependencyObject currentObject = hitTestResult.VisualHit;

            while (item == null && currentObject != null)
            {
                item = currentObject as Adorner;
                if (item != null) return true;

                currentObject = VisualTreeHelper.GetParent(currentObject);
            }

            return false;
        }
        protected ZSTreeViewItem GetTreeViewItemUnderMouse(Point positionRelativeToTree)
        {
            HitTestResult hitTestResult = VisualTreeHelper.HitTest(TreeView, positionRelativeToTree);
            if (hitTestResult == null || hitTestResult.VisualHit == null) return null;
            FrameworkElement child = hitTestResult.VisualHit as FrameworkElement;
            do
            {
                if (child is ZSTreeViewItem) return GetVisible(child as ZSTreeViewItem);
                if (child.DataContext is InsertContent) return GetVisible((child.DataContext as InsertContent).Item);
                if (child is ZSTreeView) return null;
                child = VisualTreeHelper.GetParent(child) as FrameworkElement;
            } while (child != null);

            return null;
        }


        private ZSTreeViewItem GetVisible(ZSTreeViewItem TreeListViewItem)
        {
            if (TreeListViewItem.IsVisible) return TreeListViewItem;
            return null;
        }

    }
}
